﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Docking = WeifenLuo.WinFormsUI.Docking;
using EdytorWF.Objects;

namespace EdytorWF
{
    public partial class ObjectsDlg : Docking::DockContent
    {
        private IObject SelectedObject;
        public ObjectsDlg()
        {
            InitializeComponent();

            AllowEndUserDocking = true;
            AutoScaleDimensions = new SizeF(6, 13);
            AutoScaleMode = AutoScaleMode.Font;
            ClientSize = new Size(1000, 1000);
            DockAreas = (Docking.DockAreas)(((((Docking.DockAreas.Float
            | Docking.DockAreas.DockTop)
            | Docking.DockAreas.DockBottom)
            | Docking.DockAreas.DockRight)
            | Docking.DockAreas.DockLeft)
            | Docking.DockAreas.Document);
            Name = "Objects Dialog";
            ShowHint = Docking.DockState.DockRight;
            Text = "Objects Dialog";
            TabText = "Objects Dialog";
            SelectedObject = null;
        }

        //////////////////////////////////////////////////////////////////////////////////////////////////////////
        protected override void OnLoad(EventArgs e)
        {
            base.OnLoad(e);

            LoadObjects();
        }

        //////////////////////////////////////////////////////////////////////////////////////////////////////////
        public void RefreshObjectList()
        {
            ObjectsTree.Nodes.Clear();
            LoadObjects();
        }

        //////////////////////////////////////////////////////////////////////////////////////////////////////////
        public void SelectObject(IObject obj)
        {
            foreach (TreeNode n in ObjectsTree.Nodes)
            {
                if (n.Text == obj.ObjectName)
                {   
                    ObjectProperty.SelectedObject = obj;
                    ObjectProperty.Refresh();
                    SelectedObject = obj;
                    ObjectsTree.SelectedNode = n;
                }
            }
        }

        //////////////////////////////////////////////////////////////////////////////////////////////////////////
        public IObject GetSelectedObject()
        {
            return SelectedObject;
        }

        //////////////////////////////////////////////////////////////////////////////////////////////////////////
        private void LoadObjects()
        {
            foreach (IObject o in IEngine.EngineInstance.GetObjectList())
            {
                List<TreeNode> nodes = new List<TreeNode>();

                if (o.GetTypeName() == "_TERRAIN")
                {
                    Terrain t = (Terrain)o;
                    ObjectProperty.SelectedObject = t;
                    SelectedObject = o;
                }
                else if (o.GetTypeName() == "_MODEL_OBJECT")
                {
                    ModelObject t = (ModelObject)o;
                    ObjectProperty.SelectedObject = t;
                    SelectedObject = o;
                }
                else if (o.GetTypeName() == "_SOUND_OBJECT")
                {
                    SoundObject t = (SoundObject)o;
                    ObjectProperty.SelectedObject = t;
                    SelectedObject = o;
                }
                else if (o.GetTypeName() == "_LIGHT_OBJECT")
                {
                    LightObject t = (LightObject)o;
                    ObjectProperty.SelectedObject = t;
                    SelectedObject = o;
                }
                else if (o.GetTypeName() == "_PARTICLE_OBJECT")
                {                  
                    ObjectProperty.SelectedObject = o;
                    SelectedObject = o;
                }
                else
                {
                    SelectedObject = null;
                }
                TreeNode treeNode = new TreeNode(o.ObjectName, nodes.ToArray());
                ObjectsTree.Nodes.Add(treeNode);

            }
        }

        //////////////////////////////////////////////////////////////////////////////////////////////////////////
        public void SetObjectTree(ref TreeView tw) 
        {
            ObjectsTree = tw;
        }

        //////////////////////////////////////////////////////////////////////////////////////////////////////////
        private void refresh_button_Click(object sender, EventArgs e)
        {
            RefreshObjectList();
        }

        //////////////////////////////////////////////////////////////////////////////////////////////////////////
        private void ObjectsTree_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Delete && SelectedObject != null)
            {
                IEngine.EngineInstance.RemoveObject(SelectedObject);
                RefreshObjectList();
            }
        }

        //////////////////////////////////////////////////////////////////////////////////////////////////////////
        private void ObjectsDlg_MouseEnter(object sender, EventArgs e)
        {
            ObjectsTree.Focus();
        }

        //////////////////////////////////////////////////////////////////////////////////////////////////////////
        private void ObjectsTree_AfterSelect(object sender, TreeViewEventArgs e)
        {
            TreeNode n = ObjectsTree.SelectedNode;

            foreach (IObject o in IEngine.EngineInstance.GetObjectList())
            {
                if (o.ObjectName == n.Text)
                {
                    if (o.GetTypeName() == "_TERRAIN")
                    {
                        Terrain t = (Terrain)o;
                        ObjectProperty.SelectedObject = t;
                        SelectedObject = o;
                        return;
                    }
                    else if (o.GetTypeName() == "_MODEL_OBJECT")
                    {
                        ModelObject t = (ModelObject)o;
                        ObjectProperty.SelectedObject = t;
                        SelectedObject = o;
                        return;
                    }
                    else if (o.GetTypeName() == "_SOUND_OBJECT")
                    {
                        SoundObject t = (SoundObject)o;
                        ObjectProperty.SelectedObject = t;
                        SelectedObject = o;
                        return;
                    }
                    else if (o.GetTypeName() == "_LIGHT_OBJECT")
                    {
                        LightObject t = (LightObject)o;
                        ObjectProperty.SelectedObject = t;
                        SelectedObject = o;
                        return;
                    }
                }
            }

            SelectedObject = null;
        }
        //////////////////////////////////////////////////////////////////////////////////////////////////////////
    }
}
